Shader "Study/Chapter3/NumRoll"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		[Toggle]_Open("Open",int) = 0
		_Speed("Speed",Range(0,2)) = 1
		_Row("Row",float) = 9
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
			float _Speed;
			float _Row;
			float _Open;
			float _CurVal;

			// shader 自己滑动
			fixed2 RollByShader(fixed2 uv)
			{
				fixed val = 1/_Row;
				fixed2 offset = fixed2(0,0);
				if(_Open >= 0.1)
				{
					offset = fixed2(0,(_Time.y + val) * _Speed);
				}
                return uv.xy *  fixed2(1,val) + offset;
			}

			// c# 层滑动
			fixed2 RollByCShard(fixed2 uv)
			{
				return uv.xy *  fixed2(1,1/_Row) + fixed2(0,_CurVal);
			}

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);

				// shader 内部自己计算
                // o.uv =  RollByShader(v.uv);
                o.uv =  RollByCShard(v.uv);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}
